<?php
/**
 * Model 基类
 * @author solar
 * @version 1.0
 */
class BaseModel 
{
	public $table;
	public $id;
	public $db;
	
	function __construct($db,$table,$id='') {
		$this->db=$db;
		$this->table=$table;
		$this->id=$id;
	}
		
	public function get($id) {
		$sql="SELECT * FROM {$this->table} WHERE {$this->id}='{$id}' LIMIT 1";
		return $this->db->fetchRow($sql);
	}
	
	public function insert($row) {
		$sqlPart='';
		foreach($row as $key=>$value) $sqlPart.=",{$key}='{$value}'";
		$sql="INSERT INTO {$this->table} SET ".substr($sqlPart,1);
		$this->db->query($sql);
		return $this->db->insertID();
	}
	
	public function update($row,$id) {
		$sqlPart='';
		foreach($row as $key=>$value) $sqlPart.=",{$key}='{$value}'";
		$sql="UPDATE {$this->table} SET ".substr($sqlPart,1)." WHERE {$this->id}='{$id}'";
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	public function remove($id) {
		$sql="DELETE FROM {$this->table} WHERE {$this->id}='{$id}' LIMIT 1";
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	public function getByField($aField) {
		foreach($aField as $key=>$value) $aCondition[]="{$key}='{$value}'";
		$sql="SELECT * FROM {$this->table} WHERE ".implode(' AND ',$aCondition);
		return $this->db->fetchRow($sql);
	}
}

?>